package com.yds.baseproject_yds

import android.Manifest
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Environment
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.tencent.mmkv.MMKV
import com.yds.developtools.utils.YDSLogHelper.i
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    val mList = mutableListOf<String>()
    private var defaultMMKV: MMKV? = null
    private var customMmkv: MMKV? = null

    // 要检查的权限列表
    var permissions = arrayOf(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_WIFI_STATE
    )


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        GlobalScope.launch {

            delay(5000)

        }

        findViewById<AppCompatTextView>(R.id.mTv).setOnClickListener {
//            mList[9]
            val result = customMmkv?.decodeString("YDS")
            Log.i("YDSimon", "result = $result")
        }

        requestPermissions()
    }

    /*** 作者: YDS 时间: 2025/4/9 19:32 描述: 请求权限  */
    private fun requestPermissions() {
        // 存储需要请求的权限
        val permissionsToRequest: MutableList<String> = ArrayList()

        // 检查每个权限
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionsToRequest.add(permission)
            }
        }

        // 如果有需要请求的权限
        if (permissionsToRequest.isNotEmpty()) {
            ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(), MULTIPLE_PERMISSIONS_REQUEST_CODE)
        } else {
            i("所有权限都已授予")
            MMKV.initialize(Environment.getExternalStorageDirectory().absolutePath + "/Android/mmkv/")
             defaultMMKV = MMKV.defaultMMKV()
             customMmkv = MMKV.mmkvWithID("YDSimon", MMKV.MULTI_PROCESS_MODE)

            GlobalScope.launch {
                defaultMMKV?.encode("YDS", "simon")
                customMmkv?.encode("YDS", "111111111")
                delay(5000)
                val result = defaultMMKV?.decodeString("YDS")
                Log.i("YDSimon", "result = $result")
            }
        }
    }

    private val MULTIPLE_PERMISSIONS_REQUEST_CODE = 1

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == MULTIPLE_PERMISSIONS_REQUEST_CODE) {
            var allPermissionsGranted = true
            for (result in grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allPermissionsGranted = false
                    break
                }
            }
            if (allPermissionsGranted) {
                // 所有请求的权限都已授予
                requestPermissions()
            } else {
                // 有部分权限未被授予
                val builder: AlertDialog.Builder = AlertDialog.Builder(this).setTitle("权限管理")
                    .setMessage("请接受授权申请，否则APP将无法正常工作。").setPositiveButton("确定", DialogInterface.OnClickListener { dialogInterface, i -> requestPermissions() })
                builder.create().show()
            }
        }
    }


}